﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;

namespace YArchitech.HVAC
{
	public class EquipmentLoop
	{
		public EquipmentLoop(Element ep, double radius, int lpindex)
		{
			this.rateUnbalance = 0.0;
			this.equipment = ep;
			this.index = lpindex;
			this.diameter = 2.0 * radius * 304.8;
			this.supplyPipes = new PipeSet();
			this.backPipes = new PipeSet();
			this.calcPipes = new PipeSet();
		}

		public Element Equipment
		{
			get
			{
				return this.equipment;
			}
			set
			{
				this.equipment = value;
			}
		}

		public string EquipmentName
		{
			get
			{
				return this.GetEquipmentType();
			}
		}

		public int Index
		{
			get
			{
				return this.index;
			}
			set
			{
				this.index = value;
			}
		}

		public string Name
		{
			get
			{
				return "散热器" + this.index;
			}
		}

		public int Diameter
		{
			get
			{
				return Convert.ToInt32(this.diameter);
			}
			set
			{
				this.diameter = (double)value;
			}
		}

		public double Resistance
		{
			get
			{
				return this.resistance;
			}
			set
			{
				this.resistance = value;
			}
		}

		public double EquipmentResistance
		{
			get
			{
				return this.eqResistance;
			}
			set
			{
				this.eqResistance = value;
			}
		}

		public double RateUnbalance
		{
			get
			{
				return this.rateUnbalance;
			}
			set
			{
				this.rateUnbalance = value;
			}
		}

		public PipeSet SupplyPipeSet
		{
			get
			{
				return this.supplyPipes;
			}
			set
			{
				this.supplyPipes = value;
			}
		}

		public PipeSet BackPipeSet
		{
			get
			{
				return this.backPipes;
			}
			set
			{
				this.backPipes = value;
			}
		}

		public List<Element> Elements
		{
			get
			{
				return this.GetAllElements();
			}
		}

		public double ResistTcValve
		{
			get
			{
				return this.resistTcValve;
			}
			set
			{
				this.resistTcValve = value;
			}
		}

		private List<Element> GetAllElements()
		{
			List<Element> list = new List<Element>();
			list.Add(this.equipment);
			foreach (HyCalcPipe hyCalcPipe in this.calcPipes)
			{
				list.AddRange(hyCalcPipe.Elements);
			}
			return list;
		}

		public double CalcResistance()
		{
			this.resistance = this.eqResistance * 1000.0;
			this.resistance += this.supplyPipes.Sum((HyCalcPipe hyCalcPipe) => hyCalcPipe.Resistance);
			this.resistance += this.backPipes.Sum((HyCalcPipe hypipe) => hypipe.Resistance);
			Log.Info(new string[]
			{
				this.Name + ": resistance = " + this.resistance
			});
			return this.resistance;
		}

		public void Add(HyCalcPipe pipe, bool bSupply = true)
		{
			if (bSupply)
			{
				this.supplyPipes.Add(pipe);
			}
			else
			{
				this.backPipes.Add(pipe);
			}
			this.calcPipes.Add(pipe);
		}

		private string GetEquipmentType()
		{
			return (this.equipment as FamilyInstance).Symbol.Family.Name;
		}

		public void Update()
		{
			this.supplyPipes.UpDate();
			this.backPipes.UpDate();
		}

		public void BackRevit()
		{
			this.supplyPipes.BackRevit();
			this.backPipes.BackRevit();
		}

		private Element equipment;

		private int index = 1;

		private PipeSet calcPipes;

		private double resistance;

		private double eqResistance;

		private double rateUnbalance;

		private PipeSet supplyPipes;

		private PipeSet backPipes;

		private double resistTcValve;

		private double diameter;
	}
}
